package com.cdeledu.thread4.c03;

import java.util.concurrent.TimeUnit;

public class IsInterruptedTest2 {

	public static void main(String[] args) throws InterruptedException {
		Thread thread = new Thread() {
			@Override
			public void run() {
				while (true) {
					try {
						TimeUnit.MINUTES.sleep(1);
					} catch (InterruptedException e) {
						// ignore the exception
						// here the interrupt flag will be clear.
						System.out.printf("I am be interrupted ? %s\n", isInterrupted());
					}
				}
			}
		};
		thread.setDaemon(true);
		thread.start();
		TimeUnit.MILLISECONDS.sleep(2);
		System.out.printf("Thread is interrupted ? %s\n", thread.isInterrupted());
		thread.interrupt();
		TimeUnit.MILLISECONDS.sleep(2);
		System.out.printf("Thread is interrupted ? %s\n", thread.isInterrupted());
	}
	// 由于在run方法中使用了sleep这个可中断方法，它会捕获到中断信号，并且会擦除interrupt标识，因此程序的执行结果都会是false，程序输出如下：
	// Thread is interrupted ? false
	// I am be interrupted ? false
	// Thread is interrupted ? false
	// 其实这也不难理解，可中断方法捕获到了中断信号之后，为了不影响线程中其他方法的执行，将线程的interrupt标识复位是一种很合理的设计。

}
